--local orders = 0
fsof_response = function(body)
	--print("fsof body in lua:"..body)
	responses = responses + 1
	--[[
	if responses == 3 then
		 wrk.thread:stop()  
	end
	]]
	if body ~= nil and string.find(body,'"result":"0"') ==nil then
		orders = orders + 1
	--	print("failed response:"..body)
	end 
end


fsof_request = function()
	requests = requests + 1
	--[[
	local f = assert(io.open("advert.json",'r')) 
	local req  = f:read("*all") 
	f:close()
--	print("lua req:"..req)
	]]
	--local req ='{"service":"com.fenqile.socket.protocol.QqMemberProtocol","environment":"shenzhen_nanshan_1-pre-virtual_purchase","group":"*","version":"1.0.0","tracecontext":{"trace_id":"3192214625489403155099","rpc_id":"0.1","call_chain":"http://mock.fenqile.com/execute.php[192.168.4.192:80]"},"method":"check","types":["string"],"params":[{"uin":"188348478","type":1}]}'
	-- local req = '{"service":"com.fenqile.creditcard.card.api.CardService","method":"listCard","environment":"shenzhen_nanshan_1-pre","group":"project_10148173","version":"1.0.0","tracecontext":{"trace_id":"test1503650728297","rpc_id":"0.1","call_chain":"test.fenqile.cn-auto-rpc-test"},"type":"strings","params":[{"uid":"3006568"}]}'
	local req = '{"service":"com.fenqile.order.credit_line.IUserCreditGrantStatusQueryService","method":"handler","environment":"shenzhen_nanshan_1-pre","group":"*","version":"1.0.0","tracecontext":{"trace_id":"test1503651285409","rpc_id":"0.1","call_chain":"test.fenqile.cn-auto-rpc-test"},"type":"strings","params":[{"uid":3005901}]}'
	return req
end

local threads = {}
local num = 1
function setup(thread)
   thread:set("id", num)
   thread:set("orders", orders)
   table.insert(threads, thread)
   num = num + 1
end

function init(args)
   requests  = 0
   responses = 0
   orders = 0
   local msg = "thread %d created"
   print(msg:format(id))
end


function done(summary, latency, requests)
   for index, thread in ipairs(threads) do
      local id        = thread:get("id")
      local requests  = thread:get("requests")
      local responses = thread:get("responses")
	  local orders	  = thread:get("orders")
      local msg = "thread %d made %d requests and got %d responses failed %d"
      print(msg:format(id, requests, responses, orders))
   end
end
